package com.group.lyl.send.dao.impl;

import com.group.lyl.send.dao.StaffDao;
import com.group.lyl.send.model.Staff;
import com.group.lyl.send.util.ReadFile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class StaffDaoImpl implements StaffDao {

    @Autowired
    private ReadFile readFile;


    @Override
    public List<Staff> findByBirthday(Date date) {

        SimpleDateFormat simpleDateFormat= new SimpleDateFormat("MM-dd");
        String birthday = simpleDateFormat.format(date);
        List<String> list = readFile.readFile("birthday", birthday);
       // Assert.isNull(list,"查询字段不存在");
        if(list==null||list.isEmpty()){
            return null;
        }
        List<Staff> staffList = new ArrayList<>();

        for (String s : list) {
            String[] split = s.split(readFile.getSeparator());
            Staff staff = new Staff(Long.parseLong(split[0]),split[1],split[2],split[3]);
            staffList.add(staff);
        }
        return staffList;
    }

    @Override
    public void saveStaff(Staff staff) {
        Long id = staff.getId();
        String name = staff.getName();
        String birthday = staff.getBirthday();
        String mail = staff.getMail();
        String saveStr = id.toString()+readFile.getSeparator()+name+readFile.getSeparator()+birthday+readFile.getSeparator()+mail;
        readFile.saveFile(saveStr);


    }
}
